<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");

include_once("constant/CommonClass.php");
include_once("constant/CommonDao.php");

class CelebrityImpl extends CommonUtils implements CommonDao
{
	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag 	= $DBTag;		
	}
  
	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id) 
	{
		$sql  = "SELECT name, sex, birthDate, phone, mobilePhone, address, description, status, census, parentId, isGroup FROM celebrity WHERE id = $id";
		
		$dto  = null;
		$conn = null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$dto = new CelebrityDto();

				$dto->id 			= $id;
				$dto->name			= $row[0];
				$dto->sex			= $row[1];
				$dto->birthDate		= $row[2];
				$dto->phone			= $row[3];
				$dto->mobilePhone	= $row[4];
				$dto->address		= $row[5];
				$dto->description	= strip_tags($row[6]);
				$dto->status		= $row[7];
				$dto->census		= $row[8];
				
				$dto->parentId		= $row[9];
				$dto->isGroup		= $row[10];
				
			} else {
				$this->message = "get celebrity data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $dto;
	}
	
	/* get data via name id
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByName($name) 
	{
		$sql  = "SELECT name, sex, birthDate, phone, mobilePhone, address, description, status, id, census, parentId, isGroup FROM celebrity WHERE name = '" . addslashes($name) . "'";

		$conn 	= null;
		$dto 	= null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$dto = new CelebrityDto();

				$dto->id 			= $row[8];
				$dto->name			= $row[0];
				$dto->sex			= $row[1];
				$dto->birthDate		= $row[2];
				$dto->phone			= $row[3];
				$dto->mobilePhone	= $row[4];
				$dto->address		= $row[5];
				$dto->description	= strip_tags($row[6]);
				$dto->status		= $row[7];
				$dto->census		= $row[9];

				$dto->parentId		= $row[9];
				$dto->isGroup		= $row[10];
				
			} else {
				$this->message = "get celebrity data failed by name. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $dto;
	}
	
	
	public function add($dto)
	{		
		if($dto instanceof CelebrityDto){
			$sql 	 = "INSERT INTO celebrity SET createdTime = UTC_TIMESTAMP(), description='";
			$sql	.= addslashes($dto->description);
			
			$sql 	.= "', name='";
			$sql	.= addslashes($dto->name);
			
			$sql 	.= "', sex='";
			$sql	.= $dto->sex;	
	
			$sql 	.= "', birthDate='";
			$sql	.= $dto->birthDate;
	
			$sql 	.= "', phone='";
			$sql	.= $dto->phone;

			$sql	.= "', mobilePhone='";
			$sql	.= $dto->mobilePhone;

			$sql	.= "', census='";
			$sql	.= addslashes($dto->census);

			$sql	.= "', address='";
			$sql	.= addslashes($dto->address);

			$sql	.= "', status=";
			$sql	.= $dto->status;
			
			$sql 	.= ", parentId='";
			$sql	.= $dto->parentId;
	
			$sql 	.= "', isGroup='";
			$sql	.= $dto->isGroup;
			
			$sql	.= "' ON DUPLICATE KEY UPDATE description=Values(description), name=Values(name), sex=Values(sex), census=Values(census), address=Values(address)" .
					", birthDate=Values(birthDate), phone=Values(phone), mobilePhone=Values(mobilePhone), parentId=Values(parentId), isGroup=Values(isGroup)";
			
			$id = $this->insert($this->DBTag, $sql);
			
			$dto->id = $id;		
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of MerchantDomain.';
		}
		
		
		return $id;
	}
	
	public function edit($dto)
	{		
		if($dto instanceof CelebrityDto){
			$sql 	 = "UPDATE celebrity SET description='";
			$sql	.= addslashes($dto->description);
			
			$sql 	.= "', name='";
			$sql	.= addslashes($dto->name);
			
			$sql 	.= "', sex='";
			$sql	.= $dto->sex;	
	
			$sql 	.= "', birthDate='";
			$sql	.= $dto->birthDate;
	
			$sql 	.= "', phone='";
			$sql	.= $dto->phone;

			$sql	.= "', mobilePhone='";
			$sql	.= $dto->mobilePhone;

			$sql	.= "', census='";
			$sql	.= addslashes($dto->census);

			$sql	.= "', address='";
			$sql	.= addslashes($dto->address);

			$sql	.= "', status='";
			$sql	.= $dto->status;
			
			$sql 	.= "', parentId='";
			$sql	.= $dto->parentId;
	
			$sql 	.= "', isGroup='";
			$sql	.= $dto->isGroup;
			
			$sql	.= "' WHERE id = ";
			$sql	.= $dto->id;
			
			return $this->update($this->DBTag, $sql);			
		}else{
			$this->message = 'input object is not instance of CelebrityDto.';
		}
			
		return false;
	}
	
	public function addAttribute($celebrityId, $attributeId, $attributeValue)
	{
		$sql_data_array = array(
			'celebrityId' 		=> $celebrityId,
			'attributeId' 		=> $attributeId,
			'attributeValue' 	=> $attributeValue
		);

		return $this->insertArray($this->DBTag, 'celebrity_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
	}
	
	public function addCategory($celebrityId, $categoryId)
	{
		$sql = "INSERT INTO celebrity_category SET celebrityId = $celebrityId, categoryId = $categoryId  ON DUPLICATE KEY UPDATE categoryId = Values(categoryId)";		
		$this->update($this->DBTag, $sql);
	}
	
	/*
	 * @param $categories array
	 */
	public function addCategories($celebrityId, $catIds)
	{
		$sql = "DELETE FROM celebrity_category WHERE celebrityId = $celebrityId";
		
		$this->update($this->DBTag, $sql);
		
		foreach($catIds as $catId){
			$this->addCategory($celebrityId, $catId);	
		}		
	}

	public function addCelebrityGroup($celebrityId, $celeGroupId)
	{
		$sql = "INSERT INTO celebrity_group_mapping SET celebrityId = $celebrityId, celeGroupId = $celeGroupId  ON DUPLICATE KEY UPDATE celeGroupId = Values(celeGroupId)";		
		$this->update($this->DBTag, $sql);
	}
	
	/*
	 * @param $categories array
	 */
	public function addCelebrityGroups($celebrityId, $celeGroupIds)
	{
		$sql = "DELETE FROM celebrity_group_mapping WHERE celebrityId = $celebrityId";
		
		$this->update($this->DBTag, $sql);
		
		foreach($celeGroupIds as $celeGid){
			$this->addCelebrityGroup($celebrityId, $celeGid);	
		}		
	}


	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM celebrity WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getList($where = '')
	{
		$sql = "SELECT id, name FROM celebrity $where";
		
		return $this->getHash($this->DBTag, $sql);
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql 	= 'SELECT count(1) FROM celebrity' . $where;
			
			$rs  	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = "SELECT name, sex, birthDate, phone, mobilePhone, address, description, status, id, census, parentId, isGroup FROM celebrity $where $order";
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn);
				
				while($rs && $row = mysql_fetch_array($rs)){
					$dto = new CelebrityDto();
			
					$dto->name			= $row[0];
					$dto->sex			= $row[1];
					$dto->birthDate		= $row[2];
					$dto->phone			= $row[3];
					$dto->mobilePhone	= $row[4];
					$dto->address		= $row[5];
					$dto->description	= strip_tags($row[6]);
					$dto->status		= $row[7];
					$dto->id 			= $row[8];
					$dto->census		= $row[9];
							
					$dto->parentId		= $row[10];
					$dto->isGroup		= $row[11];
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}
	
	
	public function getAttributes($dto)
	{
		$arrback 	= array();
		$conn 		= null;
		$sql  		= "SELECT attributeId, attributeValue FROM celebrity_attributes WHERE celebrityId = " . $dto->id;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			while ($row = mysql_fetch_row($rs)) {
				$dto = new AttributeDto();

				$dto->sttributeId	= $row[1];
				$dto->attributeValue= $row[2];
				
				$arrback[] = $dto;
			}
			
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $arrback;
	}
	
	public function getCategoryIds($celebrityId)
	{
		$sql = "SELECT categoryId FROM celebrity_category WHERE celebrityId = $celebrityId";
		
		return $this->getColumns($this->DBTag, $sql);
	}
	
	public function getGroupList()
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql  = "SELECT id, name FROM celebrity WHERE status = 1 AND isGroup = 1";
			
			$rs  = mysql_query($sql, $conn);
			while($rs && $row = mysql_fetch_array($rs)){
				$arrback[$row[0]] = $row[1];				
			}			
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $arrback;
	}
		
	
}
?>
